package org.xbill.DNS.utils;

import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;

public class base64
{
  private static final String Base64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";

  public static String formatString(byte[] paramArrayOfByte, int paramInt, String paramString, boolean paramBoolean)
  {
    String str = toString(paramArrayOfByte);
    StringBuffer localStringBuffer = new StringBuffer();
    int i = 0;
    if (i >= str.length())
      return localStringBuffer.toString();
    localStringBuffer.append(paramString);
    if (i + paramInt >= str.length())
    {
      localStringBuffer.append(str.substring(i));
      if (paramBoolean)
        localStringBuffer.append(" )");
    }
    while (true)
    {
      while (true)
        i += paramInt;
      localStringBuffer.append(str.substring(i, i + paramInt));
      localStringBuffer.append("\n");
    }
  }

  public static byte[] fromString(String paramString)
  {
    byte[] arrayOfByte2;
    short[] arrayOfShort2;
    int l;
    byte[] arrayOfByte3;
    ByteArrayOutputStream localByteArrayOutputStream = new ByteArrayOutputStream();
    byte[] arrayOfByte1 = paramString.getBytes();
    int i = 0;
    while (true)
    {
      if (i >= arrayOfByte1.length)
      {
        arrayOfByte2 = localByteArrayOutputStream.toByteArray();
        if (arrayOfByte2.length % 4 == 0)
          break;
        arrayOfByte3 = null;
        return arrayOfByte3;
      }
      if (!(Character.isWhitespace((char)arrayOfByte1[i])))
        localByteArrayOutputStream.write(arrayOfByte1[i]);
      ++i;
    }
    localByteArrayOutputStream.reset();
    DataOutputStream localDataOutputStream = new DataOutputStream(localByteArrayOutputStream);
    int j = 0;
    while (true)
    {
      short[] arrayOfShort1;
      while (true)
      {
        while (j >= (3 + arrayOfByte2.length) / 4)
          arrayOfByte3 = localByteArrayOutputStream.toByteArray();
        arrayOfShort1 = new short[4];
        arrayOfShort2 = new short[3];
        int k = 0;
        while (true)
        {
          while (k >= 4)
          {
            arrayOfShort2[0] = (short)((arrayOfShort1[0] << 2) + (arrayOfShort1[1] >> 4));
            if (arrayOfShort1[2] != 64)
              break label203;
            arrayOfShort2[2] = -1;
            arrayOfShort2[1] = -1;
            if ((0xF & arrayOfShort1[1]) == 0)
              break label298;
            arrayOfByte3 = null;
          }
          arrayOfShort1[k] = (short)"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=".indexOf(arrayOfByte2[(k + j * 4)]);
          ++k;
        }
        label203: if (arrayOfShort1[3] != 64)
          break;
        arrayOfShort2[1] = (short)(0xFF & (arrayOfShort1[1] << 4) + (arrayOfShort1[2] >> 2));
        arrayOfShort2[2] = -1;
        if ((0x3 & arrayOfShort1[2]) == 0)
          break label298;
        arrayOfByte3 = null;
      }
      arrayOfShort2[1] = (short)(0xFF & (arrayOfShort1[1] << 4) + (arrayOfShort1[2] >> 2));
      arrayOfShort2[2] = (short)(0xFF & (arrayOfShort1[2] << 6) + arrayOfShort1[3]);
      label298: l = 0;
      if (l < 3)
        break;
      label307: ++j;
    }
    try
    {
      if (arrayOfShort2[l] >= 0)
        localDataOutputStream.writeByte(arrayOfShort2[l]);
      ++l;
    }
    catch (IOException localIOException)
    {
      break label307:
    }
  }

  public static String toString(byte[] paramArrayOfByte)
  {
    int k;
    ByteArrayOutputStream localByteArrayOutputStream = new ByteArrayOutputStream();
    int i = 0;
    if (i >= (2 + paramArrayOfByte.length) / 3)
      return new String(localByteArrayOutputStream.toByteArray());
    short[] arrayOfShort1 = new short[3];
    short[] arrayOfShort2 = new short[4];
    int j = 0;
    if (j >= 3)
    {
      arrayOfShort2[0] = (short)(arrayOfShort1[0] >> 2);
      if (arrayOfShort1[1] != -1)
        break label158;
      arrayOfShort2[1] = (short)((0x3 & arrayOfShort1[0]) << 4);
      if (arrayOfShort1[1] != -1)
        break label179;
      arrayOfShort2[3] = 64;
      arrayOfShort2[2] = 64;
      k = 0;
    }
    while (true)
    {
      while (true)
      {
        while (true)
        {
          while (true)
          {
            while (true)
            {
              if (k < 4)
                break label243;
              ++i;
            }
            if (j + i * 3 < paramArrayOfByte.length)
              arrayOfShort1[j] = (short)(0xFF & paramArrayOfByte[(j + i * 3)]);
            while (true)
            {
              while (true)
                ++j;
              arrayOfShort1[j] = -1;
            }
            label158: arrayOfShort2[1] = (short)(((0x3 & arrayOfShort1[0]) << 4) + (arrayOfShort1[1] >> 4));
          }
          label179: if (arrayOfShort1[2] != -1)
            break;
          arrayOfShort2[2] = (short)((0xF & arrayOfShort1[1]) << 2);
          arrayOfShort2[3] = 64;
        }
        arrayOfShort2[2] = (short)(((0xF & arrayOfShort1[1]) << 2) + (arrayOfShort1[2] >> 6));
        arrayOfShort2[3] = (short)(0x3F & arrayOfShort1[2]);
      }
      label243: localByteArrayOutputStream.write("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=".charAt(arrayOfShort2[k]));
      ++k;
    }
  }
}